package com.zjw.forum.services.impl;

import com.zjw.forum.common.AppResult;
import com.zjw.forum.common.ResultCode;
import com.zjw.forum.dao.ArticleMapper;
import com.zjw.forum.dao.BoardMapper;
import com.zjw.forum.exception.ApplicationException;
import com.zjw.forum.model.Article;
import com.zjw.forum.model.Board;
import com.zjw.forum.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {
    @Autowired
    private BoardMapper boardMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public List<Board> getBoardName(Integer rows) {
        if(rows==null||rows<=0){
            log.error(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //对返回结果进行排序
        List<Board> boards = boardMapper.selectByRows(rows);
        return boards;
    }

    @Override
    public int addArticleCount(Long id) {
        //校验参数
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //根据boardId，查询对应的board
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //查询到board，开始修改这个board的articlecount
        Board board1=new Board();
        board1.setId(board.getId());
        board1.setArticleCount(board.getArticleCount()+1);
        //更新 调用者对结果判断
        int i = boardMapper.updateByPrimaryKeySelective(board1);
        return i;
    }

    @Override
    public Board getBoardInfoById(Long id) {
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null||board.getState()==1||board.getDeleteState()==1){
            log.error(ResultCode.FAILED_BOARD_BANNED.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //帖子数量的漏洞，处理一下  deletestate=1都不计数
        List<Article> articles = articleMapper.selectByBoardId(board.getId());
        int count=0;
        for (Article article:articles) {
            if(article.getDeleteState()==0){
                count++;
            }
        }
        board.setArticleCount(count);
        return board;
    }

    @Override
    public int subArticleCount(Long id) {
        //校验参数
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //根据boardId，查询对应的board
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //查询到board，开始修改这个board的articlecount
        Board board1=new Board();
        board1.setId(board.getId());
        board1.setArticleCount(board.getArticleCount()-1);
        if(board1.getArticleCount()<0){
            board1.setArticleCount(0);
        }
        //更新 调用者对结果判断
        int i = boardMapper.updateByPrimaryKeySelective(board1);
        return i;
    }

}
